/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package documenteditor.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author gleidson.moura
 */
public class AppUtil {

    /**
     * Clona o objeto e seus filhos (Clonagem profunda), para isso o objeto precisa ser serializavel
     * @param oldObj
     * @return
     * @throws Exception
     * @author gleidson.moura
     */
    public Object deepCopy(Object oldObj) throws Exception {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            // serializa e passa o objeto para o output
            oos.writeObject(oldObj);
            oos.flush();
            ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bin);

            return ois.readObject();
        } catch (Exception e) {
            throw (e);
        } finally {
            oos.close();
            ois.close();
        }
    }
}
